import 'package:flutter/material.dart';

import '../AppContants.dart';

class HomeWidget extends StatefulWidget {
  const HomeWidget({Key? key}) : super(key: key);

  @override
  State<HomeWidget> createState() => _HomeWidgetState();
}

class _HomeWidgetState extends State<HomeWidget> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton.icon(
          onPressed: () {
            // Navigator.push(context, MaterialPageRoute(builder: (context) {
            //   return const A();
            // }));
            Navigator.pushNamed(context, A_PAGE);
          },
          icon: const Icon(Icons.home),
          label: const Text("Home 2 A")),
    );
  }
}

class A extends StatefulWidget {
  const A({Key? key}) : super(key: key);

  @override
  State<A> createState() => _AState();
}

class _AState extends State<A> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(
            child: const Icon(Icons.arrow_back),
            onTap: () {
              Navigator.pop(context);
            }),
        title: const Text("A"),
      ),
      body: Center(
        child: ElevatedButton.icon(
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return const B();
              }));
            },
            icon: const Icon(Icons.home),
            label: const Text("A to B")),
      ),
    );
  }
}

class B extends StatefulWidget {
  const B({Key? key}) : super(key: key);

  @override
  State<B> createState() => _BState();
}

class _BState extends State<B> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(
            child: const Icon(Icons.arrow_back),
            onTap: () {
              Navigator.pop(context);
            }),
        title: const Text("B"),
      ),
      body: Center(
        child: ElevatedButton.icon(
            onPressed: () {
             var result =  Navigator.push(context, MaterialPageRoute(builder: (context) {
                return const C();
              }));
             result.then((value) => print("back value is = $value"));
            },
            icon: const Icon(Icons.home),
            label: const Text("B to C")),
      ),
    );
  }
}

class C extends StatefulWidget {
  const C({Key? key}) : super(key: key);

  @override
  State<C> createState() => _CState();
}

class _CState extends State<C> {
  ///退出
  void pop(String back) {
    Navigator.pop(context, back);//传参数
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(
            child: const Icon(Icons.arrow_back),
            onTap: () {
              pop("C title back");
            }),
        title: const Text("C"),
      ),
      body: Center(
        child: ElevatedButton.icon(
            onPressed: () {
              pop("C content back");
            },
            icon: const Icon(Icons.home),
            label: const Text("pop")),
      ),
    );
  }
}
